﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using JLL.China.Model;
using JLL.China.Model.DTO;
using LINQ = JLL.China.Infrastructure.Repositories.LINQ2SQL;
using System.IO;
using System.Web;
using System.Web.Configuration;

namespace JLL.China.Infrastructure.Repositories.ProjectDirectoryMedia
{
    public class ProjectDirectoryMediaRepository : SQLRepositoryBase<Model.ProjectDirectoryMedia, int>, IProjectDirectoryMediaRepository
    {
        public int CountItem { get; set; }
   
        public override Model.ProjectDirectoryMedia FindBy(int key)
        {
            var listing = DataContext.ProjectDirectoryMedias.SingleOrDefault(x => key.Equals(x.Media_ID));

            if (listing != null)
            {
                return ProjectDirectoryMediaFactory.FromLINQObject(listing);
            }
            else
            {
                return null;
            }

        }

        public decimal FindByListingIDImageType(decimal id,string imageType)
        {
            var listing = DataContext.ProjectDirectoryMedias.SingleOrDefault(x => x.ListingsFID.Equals(id) && x.ImgType.Equals(imageType));

            if (listing != null)
            {
                //var res = ProjectDirectoryMediaFactory.FromLINQObject(listing);
                return listing.Media_ID;
            }
            else
            {
                return 0;
            }

        }
        
        //
        public int GetID(Guid guid)
        {
            var item = DataContext.ProjectDirectoryMedias.SingleOrDefault(x => guid.Equals(x.GuiID));

            if (item != null)
            {
                return (int)item.Media_ID;
            }
            else
            {
                return 0;
            }
        }


        public override IList<Model.ProjectDirectoryMedia> FindAll()
        {
            throw new NotImplementedException();
        }

        protected override void PersistNewItem(Model.ProjectDirectoryMedia item)
        {
           DataContext.ProjectDirectoryMedias.InsertOnSubmit(ProjectDirectoryMediaFactory.ToLINQObject(item));
        }
        //
        
        //
        protected override void PersistUpdatedItem(Model.ProjectDirectoryMedia item)
        {

            var existItem = DataContext.ProjectDirectoryMedias.SingleOrDefault(x=>x.Media_ID.Equals(item.ID));
            if(existItem != null){
                var it = item;
                existItem.Filename = item.Filename;
                existItem.ListingsFID = item.ListingsFID;
                existItem.IsActive = item.IsActive;
                existItem.IsDefault = item.IsDefault;
                existItem.FileData = item.FileData;
                existItem.Orders = item.Orders;
                existItem.TypeOfUpload = existItem.TypeOfUpload;
            }
        }
        protected override void PersistDeletedItem(Model.ProjectDirectoryMedia item)
        {
            var media = DataContext.ProjectDirectoryMedias.SingleOrDefault(x => x.Media_ID.Equals(item.ID));
            if (media != null)
            {

               
                
                //Delete image
                string filenamePath = HttpContext.Current.Request.PhysicalApplicationPath+(WebConfigurationManager.AppSettings["ImgDirectoryFolder"].ToString() + media.ListingsFID.ToString() + "\\" + media.Filename);
                
                if(File.Exists(filenamePath))
                {
                    File.Delete(filenamePath);
                    string thumbpath = HttpContext.Current.Request.PhysicalApplicationPath + (WebConfigurationManager.AppSettings["ImgDirectoryFolder"].ToString() + media.ListingsFID.ToString() + "\\thumb\\" + media.Filename);
                    File.Delete(thumbpath);
                }
                DataContext.ProjectDirectoryMedias.DeleteOnSubmit(media);
              
            }
        }
        public IList<Model.ProjectDirectoryMedia> FindById(int id)
        {
            var items = from m in DataContext.ProjectDirectoryMedias
                        where m.ListingsFID == id
                        orderby m.Orders ascending 
                        select m;
            var list = items.ToList();
            var result = new List<Model.ProjectDirectoryMedia>();
            foreach (var n in list)
            {
                result.Add(ProjectDirectoryMediaFactory.FromLINQObject(n));
            }
            //Count
            this.CountItem =result.Count;
            //
            return result;

        }
        public void ChangeOrder(int id1,int order1, int id2,int order2)
        {
            DataContext.sp_projectdir_change_order(id1, order1, id2, order2);
        }
        
    }
}
